package com.atguigu.jxc.controller;

import com.atguigu.jxc.dao.*;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SupplierService;
import com.atguigu.jxc.service.UserService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/returnListGoods")
public class ReturnListGoodsController {

    @Autowired
    private UserDao userDao;
    @Autowired
    private ReturnListDao returnListDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private SupplierDao supplierDao;

    /**
     * 退货单保存
     * http://localhost:8080/returnListGoods/save?returnNumber=TH1605768591211（退货单号前端生成）
     */
    @PostMapping("/save")
    @ResponseBody
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr){

        if(returnList == null || returnListGoodsStr == null){
            return null;
        }

        Gson gson = new Gson();
        List<ReturnListGoods> ReturnGoodsList = gson.fromJson(returnListGoodsStr, new TypeToken<List<ReturnListGoods>>() {}.getType());
        // 设置当前操作用户
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());

        returnList.setUserId(currentUser.getUserId());

        returnListDao.insertReturnList(returnList);

        for (ReturnListGoods returnListGoods : ReturnGoodsList) {
            returnListGoods.setReturnListId(returnList.getReturnListId());

            returnListDao.insertReturnListGoods(returnListGoods);

            // 修改商品库存，状态
            Goods goods = goodsDao.findByGoodsId(returnListGoods.getGoodsId());

            goods.setInventoryQuantity(goods.getInventoryQuantity()+returnListGoods.getGoodsNum());

            goods.setState(2);

            goodsDao.updateGoods(goods);
        }


        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**退货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
     * http://localhost:8080/returnListGoods/list
     */
    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> findReturnList(String returnNumber,Integer supplierId, Integer state,String sTime,String eTime){
        List<ReturnList> list = returnListDao.findReturnList(returnNumber,supplierId,state,sTime,eTime);
        if(list == null){
            return null;
        }
        for (ReturnList returnList : list) {
            Supplier supplierById = supplierDao.getSupplierById(returnList.getSupplierId());
            User userById = userDao.getUserById(returnList.getUserId());

            returnList.setSupplierId(supplierById.getSupplierId());
            returnList.setTrueName(userById.getTrueName());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    /**退货单商品信息
     *http://localhost:8080/returnListGoods/goodsList
     */
    @PostMapping("/goodsList")
    @ResponseBody
    public Map<String,Object> findReturnGoodsList(Integer returnListId){
        List returnListGoodsList = returnListDao.findReturnGoodsList(returnListId);
        if (returnListGoodsList == null){
            return null;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("rows", returnListGoodsList);
        return map;
    }

    /**删除退货单
     *http://localhost:8080/returnListGoods/delete
     */
    @PostMapping("/delete")
    @ResponseBody
    public ServiceVO deleteReturnList(Integer returnListId){
        returnListDao.deleteReturnList(returnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**退货统计（可根据 商品类别、商品编码或名称 条件查询）
     *http://localhost:8080/returnListGoods/count
     */
    @PostMapping("/count")
    @ResponseBody
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        List list = returnListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        if (list == null){
            return null;
        }
        return list.toString();
    }
}
